OpenWrt 上通过 WebDAV 共享文件 By LOOKAS2001

您所在的位置:网站首页 openwrt 文件共享 外网 OpenWrt 上通过 WebDAV 共享文件 By LOOKAS2001

OpenWrt 上通过 WebDAV 共享文件 By LOOKAS2001

2024-01-09 10:49| 来源: 网络整理| 查看: 265

lookas2001 版权所有,本作品采用知识共享署名 4.0 国际许可协议进行许可,转载使用的时候请注明作者以及来源。

OpenWrt ( https://openwrt.org/ ) 是一个蛮强大的路由器固件,通过安装软件包可以实现很多功能。WebDAV ( http://www.webdav.org/ ) 是一个对 HTTP 的拓展,可用于共享文件。于是乎,我们可以尝试在 OpenWrt 上安装相应的软件包,让设备支持 WebDAV。

相比于 SMB, AFP,在实际测试中,WebDAV 的速度比较占优势。这点可能得益于 WebDAV 是基于 HTTP 的,HTTP 服务端可能有一些黑科技在降低占用的时候提高速度(也有可能是接下来的步骤中的 WebDAV 是基于 http 而不是 https 的原因)。

另外写这一篇文章的原因是 SMB 和 AFP 已经有了比较完善的教程,比如这两篇文章 https://openwrt.org/docs/guide-user/services/nas/samba_configuration https://openwrt.org/docs/guide-user/services/nas/netatalk_configuration 但是 WebDAV 在文档方面就比较缺乏。

Lighttpd ( https://www.lighttpd.net/ ) 是一个轻量级的,但是功能较为完备的 HTTP 服务端,观察到他提供了 WebDAV mod ,故可用其来实现 WebDAV 服务器。

安装 Lighttpd 以及 WebDAV Auth 模块

先 opkg update 来更新本地的软件包信息。

通过 opkg install lighttpd lighttpd-mod-webdav lighttpd-mod-auth lighttpd-mod-authn_file 可将所依赖的软件包一键装齐。

如果出现了下载速度慢或者下载遇到困难,可以手动到 http://downloads.openwrt.org 上下载对应的包然后安装,或者设置一下网络代理(这不属于这篇文章的谈论范围,需要你自己想办法啦)。

配置 Lighttpd

不像 SMB 提供了 uci 统一配置的接口,Lighttpd 需要在 /etc/lighttpd 下修改。

通过 vi /etc/lighttpd/lighttpd.conf 打开 lighttpd 的主配置文件。

可通过 cp /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.bak 设置一个备份,便于配置出错的时候还原。

这是一份配置过的配置文件:

server.document-root = "/mnt" server.upload-dirs = ( "/tmp" ) server.errorlog = "/var/log/lighttpd/error.log" server.pid-file = "/var/run/lighttpd.pid" server.username = "http" server.groupname = "www-data" index-file.names = ( "index.php", "index.html", "index.htm", "default.htm", ) static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) ### Options that are useful but not always necessary: #server.chroot = "/" server.port = 81 #server.bind = "localhost" #server.tag = "lighttpd" server.errorlog-use-syslog = "enable" #server.network-backend = "writev" ### Use IPv6 if available #include_shell "/usr/share/lighttpd/use-ipv6.pl" dir-listing.encoding = "utf-8" server.dir-listing = "enable" include "/etc/lighttpd/mime.conf" include "/etc/lighttpd/conf.d/*.conf"

lighttpd 配置文件中注释是通过在行前加入“#”来实现的。

这里修改了几点:

server.document-root = "/mnt" ,即将文档根目录设置为 /mnt ,我为路由器添加了两个硬盘,分别挂载在 /mnt/sda1 和 /mnt/sdb1 下,这个存放位置不是固定的,可以根据你自己的喜好调整。

server.port = 81 ,即后面我们用来访问的端口,80 端口已经被系统自带的 uHTTPd 占用了,这里设置另外一个防止冲突。

server.errorlog-use-syslog = "enable" ,这个选项可以将错误日志输出到 syslog ,便于我们在 web 控制台查看错误。

server.dir-listing = "enable" , dir-listing.encoding = "utf-8" ,这两个选项可以启用列出文件功能,并且防止文件名乱码。

配置 WebDAV 模块

通过 vi /etc/lighttpd/conf.d/30-webdav.conf 打开 lighttpd 的主配置文件。

这是一份配置过的配置文件:

####################################################################### ## ## WebDAV Module ## --------------- ## ## See https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModWebDAV ## server.modules += ( "mod_webdav" ) #$HTTP["url"] =~ "^/dav($|/)" { ## ## enable webdav for this location ## webdav.activate = "enable" ## ## By default the webdav url is writable. ## Uncomment the following line if you want to make it readonly. ## webdav.is-readonly = "enable" ## ## Log the XML Request bodies for debugging ## #webdav.log-xml = "disable" ## ## ## webdav.sqlite-db-name = "/tmp/lighttpd-webdav.db" #} ## #######################################################################

这里修改了几点:

注释掉了 $HTTP["url"] =~ "^/dav($|/)" { , } 两行,这里安装 Lighttpd 的目的就是为了 WebDAV ,注释掉这两行可以将整个网站都设置为 WebDAV 。

webdav.activate = "enable" ,为整个站点启用了 WebDAV 。

webdav.is-readonly = "enable" ,设置运行模式是只读模式,这里设置 disable 可以禁用只读(即可写可读)。

"/mnt/sda1/.lighttpd-webdav.db" ,这里需要为 WebDAV 模块设置一个数据库存储位置,位置建议选择在硬盘上,这个数据库文件需要存储的除了锁定还有一些属性,如果存储在易丢失的地方(如 /tmp )会导致数据丢失,存储上除硬盘以外的位置会缩短闪存寿命(闪存有擦除上限),请注意,Lighttpd 需要对存储位置的目录有写入的权限,可用 chmod a+w xxx,来授予权限。

RefOpenWrt 论坛上的内容 https://forum.openwrt.org/t/webdav-configuration-essense-with-lighttpd-on-openwrt/25357Lighttpd 提供的文档 https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModWebDAV配置 Auth 模块

这块的配置是用于提升你的文件安全性的,但并不是必须的,而且这方面的配置只可提升少许安全性,攻击者仍然可以在中途截获密码,若想更好的提升安全性,请配置 HTTPS 。

通过 vi /etc/lighttpd/conf.d/20-auth.conf 打开 lighttpd 的主配置文件。

这是一份配置过的配置文件:

####################################################################### ## ## Authentication Module ## ----------------------- ## ## See https://redmine.lighttpd.net/projects/lighttpd/wiki/docs_modauth ## for more info. ## server.modules += ( "mod_auth" ) auth.backend = "plain" auth.backend.plain.userfile = "/etc/lighttpd/lighttpd.user" #auth.backend.plain.groupfile = "/etc/lighttpd/lighttpd.group" #auth.backend.ldap.hostname = "localhost" #auth.backend.ldap.base-dn = "dc=my-domain,dc=com" #auth.backend.ldap.filter = "(uid=$)" auth.require = ( "/" => ( "method" => "basic", "realm" => "Personal File Server", "require" => "valid-user" ), ) ## #######################################################################

这里修改了几点:

可能是包打包人员的疏忽,原来的配置文件中没有 server.modules += ( "mod_auth" ) 一行,为了启用这个模块,须有手动加上。

auth.backend = "plain" ,设置认证后端为 plain 。

auth.backend.plain.userfile = "/etc/lighttpd/lighttpd.user" ,设置认证后端存储认证信息的位置。

auth.require = ..... ,取消这里的注释即意味着启用了认证。

"/" ,代表认证的位置,这里是全站。

"method" => "basic" ,认证的类型,这里设置为 basic 是为了更好的客户端兼容性。

"realm" => "Personal File Server" ,即认证时提示的消息,随便设置即可。

通过 touch /etc/lighttpd/lighttpd.user 可以创建我们需要的认证信息文件。

通过 vi /etc/lighttpd/lighttpd.user 编辑认证信息文件。

这是一份样例:

user1:password1 user2:password2

用户名和密码见用 : 隔开,多个用户之间用空行隔开。

RefLighttpd 提供的文档 https://redmine.lighttpd.net/projects/lighttpd/wiki/docs_modauth


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3